home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].zip / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].adf / Calendar / lunar.c < prev    next >
C/C++ Source or Header  |  1990-01-31  |  3KB  |  141 lines

  1. /*#include <curses.h>*/
  2. #include <math.h>
  3. /*#include <sys/types.h>*/
  4. #include <time.h>
  5.  
  6. /* Globals. */
  7. double           Fraction;
  8.  
  9. /* Linked in later. */
  10. char            *rindex();
  11. time_t           time();
  12. struct tm       *localtime();
  13.  
  14.  
  15. #define LINES           24
  16. #define WIDTH           80
  17. #define CENTER          ((WIDTH - 2 * LINES) / 2)
  18. #define BRIGHT          '@'
  19. #define LEDGE           '('
  20. #define REDGE           ')'
  21. #define FULL            0.5
  22. #define TwoPi           (2 * 3.14159)
  23. #define ZERO            0.03
  24.  
  25. extern short month, day, year;
  26. long Calculate();
  27.  
  28. lunar()
  29. {
  30.         long yday;
  31.  
  32.         yday = (long) (days_since_jan1(month, day, year) + 1);
  33.         Calculate(yday, ((long) (year - 1900)), 11L, 0L);
  34.         Draw();
  35.         refresh();
  36.         get_char();
  37.         clear();
  38.         print_screen();
  39. }
  40.  
  41. long
  42. Calculate(julian, year, hour, minute)
  43. long julian, year, hour, minute;
  44. {
  45.     static struct tm *tm;
  46.     register long    Length;
  47.     register long    Phase;
  48.     register long    DeltaH;
  49.     register long    Delta;
  50.     register long    offset;
  51.     /*time_t                 tick;
  52.  
  53.     static short called_before = 0;
  54.  
  55.     if (!called_before) {
  56.         tick    = time((time_t *)0);
  57.         tm              = localtime(&tick);
  58.         called_before = 1;
  59.     } else {
  60.         tm->tm_yday++;
  61.     }
  62.     julian  = tm->tm_yday + 1;
  63.     year    = tm->tm_year - 78;
  64.     hour    = tm->tm_hour;
  65.     minute  = tm->tm_min;*/
  66.  
  67.     year -= 78;
  68.     Length  = (double)2551 / 60 * 1000 + (double)443 / 60;
  69.     offset  = ((year * 365L + julian) * 24L + hour) * 60L + minute;
  70.     Delta   = offset - (273L * 24L + 13L) * 60L + 23L;
  71.     Phase   = Delta - (Delta / Length) * Length;
  72.  
  73.     Fraction        = (double)Phase / Length;
  74.     return(Phase);
  75. }
  76.  
  77. int
  78. CharPos(x)
  79.     double          x;
  80. {
  81.     register int    i;
  82.  
  83.     i = x * LINES + 0.5;
  84.     if ((i += LINES + CENTER) < 1)
  85.     i = 1;
  86.     return(i);
  87. }
  88.  
  89.  
  90. Draw()
  91. {
  92.     register char   *p;
  93.     register int     i;
  94.     register int     end;
  95.     register double  y;
  96.     register double  cht;
  97.     register double  squisher;
  98.     register double  horizon;
  99.     register double  terminator;
  100.     char             Buffer[256];
  101.     int line = 1;
  102.  
  103.     /* Clear screen? */
  104.     clear();
  105.  
  106.     if (Fraction < FULL)
  107.     squisher = cos(TwoPi * Fraction);
  108.     else
  109.     squisher = cos(TwoPi * (Fraction - FULL));
  110.  
  111.     cht = (double)2.0 / (LINES - 6.0);
  112.     for (y = 0.93; y > -1.0; y -= cht)
  113.     {
  114.     for (i = sizeof Buffer, p = Buffer; --i >= 0; )
  115.         *p++ = ' ';
  116.     horizon = sqrt(1.0 - y * y);
  117.     Buffer[    CharPos(-horizon)]   = LEDGE;
  118.     Buffer[i = CharPos( horizon)]   = REDGE;
  119.     Buffer[++i]                     = '\0';
  120.     terminator = horizon * squisher;
  121.     if (Fraction > ZERO && Fraction < (1.0 - ZERO))
  122.     {
  123.         if (Fraction < FULL)
  124.         {
  125.         i   = CharPos( terminator);
  126.         end = CharPos( horizon);
  127.         }
  128.         else
  129.         {
  130.         i   = CharPos(-horizon);
  131.         end = CharPos( terminator);
  132.         }
  133.         while (i <= end)
  134.         Buffer[i++] = BRIGHT;
  135.     }
  136.     mvaddstr(line++, 1, Buffer);
  137.     }
  138.     move(LINES-1, 0);
  139.     refresh();
  140. }
  141.